<template>
  <!-- 添加账页面-->
  <div>
    <el-card>
      <!-- 标题 -->
      <div slot="header">
        <span>添加账号</span>
      </div>
      <!-- 内容部分 -->
      <!--1. prefix-icon="xxxxxx"   输入框前面小图标 -->
      <!--2. clearable   清空输入框信息 -->
      <!--3. show-password  密码框小眼睛 -->
      <!--4. :rules="rules"  表单的验证规则要用到  prop="xxx"要对应验证规则-->
      <!--5. ref="xxxxx"  相当于id-->
      <!--6. status-icon  表单提交验验证 输入框没内容错误提示  -->
      <div class="content">
        <el-form
          :model="addAccForm"
          :rules="rules"
          ref="addAccForm"
          label-width="100px"
        >
          <el-form-item label="账号" prop="account">
            <el-input
              v-model="addAccForm.account"
              placeholder="请输入账号"
              autocomplete="off"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="addAccForm.password"
              placeholder="请输入密码"
              autocomplete="off"
              clearable
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item label="用户分组" prop="userGroup">
            <!-- 下拉框 -->
            <el-select
              v-model="addAccForm.userGroup"
              placeholder="请选择用户分组"
            >
              <el-option value="普通管理员">普通管理员</el-option>
              <el-option value="超级管理员">超级管理员</el-option>
              <el-option value="普通用户">普通用户</el-option>
              <el-option value="体验用户">体验用户</el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">添加</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { usersAdd } from "@/api/user";
export default {
  data() {
    return {
      //表单数据
      addAccForm: {
        account: "",
        password: "",
        userGroup: "",
      },
      //验证规则
      rules: {
        //账号的
        account: [
          // message  提示输入账户
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符", //错误提示
            trigger: "blur", //失去焦点触发
          },
        ],
        //密码的
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur", //失去焦点触发
          },
        ],
        userGroup: [
          { required: true, message: "请选择用户组", trigger: "blur" },
        ],
      },
    };
  },
  //方法
  methods: {
    //添加
    submitForm() {
      this.$refs.addAccForm.validate(async (valid) => {
        //点击提交按钮  找到表单  用validate()方法  表单都有值就是true
        if (valid) {
          // console.log("submit");
          let res = await usersAdd(this.addAccForm);
          let { code, msg } = res;
          // console.log(code,msg);//code:0 成功  msg:提示
          if (code === 0) {
            this.$message({
              message: msg,
              type: "success",
            });
            //添加成功 跳账户列表
            this.$router.push("/acc")
          } else {
           this.$message.error(msg);
        }
        } else {
           this.$message.error("请输入信息!");
        }
      });
    },
    // 重置
    resetForm() {
      // resetFields()---对该表单项进行重置，将其值重置为初始值并移除校验结果
      this.$refs.addAccForm.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.content {
  width: 400px;
  .el-select {
    width: 100%;
  }
}
</style> 